import {reactive} from 'vue'
import {LocationQuery} from 'vue-router'
import {blogDetail, blogEdit} from '@/api/blog'
import {env} from '@/utils/env'
import {commentCreate, commentInfo} from "@/api/comment";
import {Comment} from "@/api/comment/type";
import {changeStar} from "@/api/like";
import {useUserStore} from "@/store/modules/user";

const userStore = useUserStore();

const useBlog = () => {
    const blogData = reactive({
        uid: 0,
        title: '',
        sub_title: '',
        isStar: false,
        isShowEdit: false,
        blogInfo: {} as Blog.BlogInfo,
        blogInfoEdit: {} as Blog.BlogInfo,
    })

    const getBlogDetail = (query: LocationQuery): Promise<boolean> => {
        return new Promise((resolve, reject) => {
            let id = query.id
            let userId = query.userId
            let params = {
                uids: String(id),
                userId: String(userId),
            }
            blogDetail(params)
                .then(res => {
                    blogData.blogInfo =
                        res.data.blog_info &&
                        res.data.blog_info.map(item => {
                            item.cover = `${env.VITE_APP_IMG_URL}/${item.cover}`
                            return item
                        })[0]

                    blogData.blogInfoEdit = {...blogData.blogInfo}
                    blogData.title = res.data.blog_info[0].title
                    blogData.sub_title = res.data.blog_info[0].sub_title
                    blogData.uid = res.data.blog_info[0].uid
                    blogData.isStar = res.data.blog_info[0].star_ids.includes(userStore.info.id)

                    resolve(true)
                })
                .catch(() => {
                    reject(false)
                })
        })
    }

    const editSubmit = async () => {
        let data = {
            uid: blogData.uid,
            title: blogData.title,
            sub_title: blogData.sub_title,
            content: blogData.blogInfoEdit.content,
        }

        await blogEdit(data)
        blogData.blogInfo = {...blogData.blogInfoEdit}
        return true
    }

    const commentData = reactive({
        list: [] as Comment.CommentInfo[],
        content: '',
    })

    const getComments = (uid: number) => {
        let params = {
            page: 1,
            limit: 10,
            type: 'blog',
            content_id: uid,
        }
        commentInfo(params).then((res) => {
            commentData.list = res.data.infos.map(info => {
                return {
                    ...info,
                    user_info: {
                        ...info.user_info,
                        avatar_url: `${env.VITE_APP_IMG_URL}/${info.user_info.avatar_url}`,
                    },
                }
            })
        })
    }

    const createComment = async (uid: number, userId: number) => {
        if (commentData.content.trim() == '') {
            window.$message.error('评论内容不能为空')
            return
        }
        let data = {
            content_id: uid,
            createdUserId: userId,
            content: commentData.content,
            type: 'blog',
        }
        await commentCreate(data)
        commentData.content = ''
        getComments(uid)
        getBlogDetail({id: String(uid)})
    }

    const blogStar = async (uid: number,userId: number | string) => {
        const data = {
            uid: uid,
            createdUserId: userId,
            star_type: blogData.isStar ? 0 : 1,
            type: 2,
        }
        await changeStar(data)
        getBlogDetail({id: String(uid)})
    }

    return {
        blogData,
        getBlogDetail,
        editSubmit,
        commentData,
        getComments,
        createComment,
        blogStar
    }
}

export {useBlog}
